﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DrugStorehouseMIS.DAL;
using DrugStorehouseMIS.Comm;
using DrugStorehouseMIS.DAL.Interface;

namespace DrugStorehouseMIS.BLL
{
    public class StockOutDetail
    {
        #region 封装属性

        private string mStockOutNum;

        public string StockOutNum
        {
            get { return mStockOutNum; }
            set { mStockOutNum = value; }
        }

        private Drug mDrugNum;

        public Drug DrugNum
        {
            get { return mDrugNum; }
            set { mDrugNum = value; }
        }

        private float mOutportPrice;

        public float OutportPrice
        {
            get { return mOutportPrice; }
            set { mOutportPrice = value; }
        }

        private int mRealQuantity;

        public int RealQuantity
        {
            get { return mRealQuantity; }
            set { mRealQuantity = value; }
        }

        private float mAmount;

        public float Amount
        {
            get { return mAmount; }
            set { mAmount = value; }
        }

        #endregion

        private IStockOutDetailDAL dal;

        public StockOutDetail()
        {
            dal = Adapter.GetStockOutDetail();
        }

        /// <summary>
        /// 获取对象
        /// </summary>
        /// <param name="dr"></param>
        public void FetchObject(IDataReader dr)
        {
            if (dr != null)
            {
                mAmount = ConvertDataType.CheckFieldFloat(dr["amount"]);
                mDrugNum.DrugNum = ConvertDataType.CheckFiledInt(dr["drug_num"]);
                mOutportPrice = ConvertDataType.CheckFieldFloat(dr["outport_price"]);
                mRealQuantity = ConvertDataType.CheckFiledInt(dr["real_quantity"]);
                mStockOutNum = ConvertDataType.CheckFiledString(dr["stock_out_num"]);
            }
        }

        /// <summary>
        ///  根据单号获取,返回datatable
        /// </summary>
        /// <param name="StockInNum"></param>
        /// <returns></returns>
        public DataTable GetStockOutDeatilByNum(string num)
        {
            return dal.Select(num);
        }

        /// <summary>
        /// 获取同一单号的所有对象
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public IList<StockOutDetail> GetStockOutDetailAll(string num)
        {
            IList<StockOutDetail> list = new List<StockOutDetail>();
            IDataReader dr = dal.SelectByNum(num);
            while (dr != null && dr.Read())
            {
                StockOutDetail obj = new StockOutDetail();
                obj.DrugNum = new Drug();

                obj.FetchObject(dr);
                list.Add(obj);
            }

            return list;
        }

        /// <summary>
        /// 创建StockOutDetail对象
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool CreateStockOutDetail(StockOutDetail detail)
        {
            return dal.Insert(detail.StockOutNum, detail.DrugNum.DrugNum, detail.RealQuantity, detail.OutportPrice, detail.Amount) > 0 ? true : false;
        }

        /// <summary>
        /// 更新StockOutDetail对象
        /// </summary>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool SetStockOutDetail(StockOutDetail detail)
        {
            return dal.Update(detail.StockOutNum, detail.DrugNum.DrugNum, detail.RealQuantity, detail.OutportPrice, detail.Amount) > 0 ? true : false;
        }

        /// <summary>
        ///  根据单号删除StockOutDetail对象
        /// </summary>
        /// <param name="StockOutNum"></param>
        /// <returns></returns>
        public bool RemoveStockOutDetail(string StockOutNum)
        {
            return dal.Delete(StockOutNum) > 0 ? true : false;
        }

        /// <summary>
        ///  根据单号和药品号删除StockOutDetail对象
        /// </summary>
        /// <param name="StockOutNum"></param>
        /// <param name="DrugNum"></param>
        /// <returns></returns>
        public bool RemoveStockOutDetail(string StockOutNum,int DrugNum)
        {
            return dal.Delete(StockOutNum, DrugNum) > 0 ? true : false;
        }
    }
}
